#include "TerminalView.hpp"
#include "Item.hpp"

#include "lambda_visitor.hpp"

#define MSG_FOUND_KEY "Found new key"
#define MSG_FOUND_STORY "Found new story item"
#define MSG_FOUND_PART "Found new item"

namespace backpack {



NodeDescriptionBuilder<TerminalView> TerminalView::newNodeDescription() {
  return NodeDescriptionBuilder<TerminalView>(*this);
}


UserChoiceBuilder<TerminalView> TerminalView::newUserChoice() {
  return UserChoiceBuilder<TerminalView>(*this);
}


void TerminalView::showMessage(std::string const& message) {
  printLine(message).clearInputBuffer().readLine();
}


void TerminalView::showNewItem(ItemVariant const& item) {
  auto visitor = makeItemVisitor(util::make_lambda_visitor(
      [=] (Key const& key) {
        print(MSG_FOUND_KEY).print(": ").printLine(key.getName());
      },
      [=] (StoryItem const& storyItem) {
        print(MSG_FOUND_STORY).print(": ").printLine(storyItem.getName());
      },
      [=] (ItemPart const& itemPart) {
        print(MSG_FOUND_PART).print(": ").printLine(itemPart.getName());
      }));
  item.apply_visitor(visitor);
  readLine();
}



void NodeDescriptionBuilder<TerminalView>::waitForPlayerConfirmation() const {

  m_rView
      .clearScreen()
      .printLine(m_strNodeDesc)
      .printLine();

  std::for_each(
      m_listHotSpotDesc.begin(),
      m_listHotSpotDesc.end(),
      [&] (std::string const& description) {
        m_rView.printLine(description);
      });

  m_rView
      .clearInputBuffer()
      .printLine()
      .readLine();
}



UserActivityVariant const& UserChoiceBuilder<TerminalView>::letPlayerChoose() {

  m_rView
      .clearScreen()
      .printLine(m_strTitle)
      .printLine();

  std::for_each(
      m_mapOptions.begin(),
      m_mapOptions.end(),
      [&] (std::pair<char, OptionTuple> const& keyValuePair) {
        m_rView
            .print(keyValuePair.first)
            .print(") ")
            .printLine(keyValuePair.second.description);
      });
  m_rView.printLine();

  char chosen = -1;
  do {
    chosen = m_rView.clearInputBuffer().print("> ").readSingleChar();
  } while(m_mapOptions.find(chosen) == m_mapOptions.end());

  return m_mapOptions.find(chosen)->second.activity;
}



} // namespace backpack

